package com.nyist.myticket.core.redis;

import com.nyist.myticket.core.base.ApplicationContextFactory;
import com.nyist.myticket.web.common.controller.TbJobTaskController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author yangxiaogen
 * @date 2021/6/15 20:41
 */
public class RedisUse extends RedisFactory {

    public final static String host = "121.5.17.166";
    public final static int port = 6379;
    public final static int timeOut = 20000;
    public final static String passwordRedis = "yang.1011";

    /**
     * 保存数据
     *
     * @param key
     */
    public static void setValue(String key, String value, int expireTime) {
        Jedis redis = null;
        try {
            redis = getJedis();
            redis.set(key, value);
            redis.expire(key, expireTime);
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
    }

    /**
     * 获取值(用户ID)
     *
     * @returne
     */
    public static String getValue(String key) {
        Jedis redis = null;
        String target = null;
        try {
            redis = getJedis();
            target = redis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
        return target;
    }

    public void getRedis(String key) {
        Jedis redis = null;
        try {
            redis = new Jedis(host, port);
            redis.auth(passwordRedis);
            System.out.println(redis.get(key));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
    }

    public void getRedisPool(String key) {

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeOut, passwordRedis);
        Jedis redis = new Jedis();

        try {
            redis = jedisPool.getResource();
            System.out.println(redis.get(key));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
    }

    public static void main(String[] args) {
        /*RedisUse redisUse = new RedisUse();
        System.out.println("redis----------------");
        redisUse.getRedis("k");
        System.out.println("pool--------------------");
        redisUse.getRedisPool("k");*/

        System.out.println(getValue("k"));
    }
}

class getBeanTest {
    public static void main(String[] args) {
        Object bean = ApplicationContextFactory.getBean(TbJobTaskController.class);
        if (bean != null) {
            System.out.println(bean);
        } else {
            System.out.println("没有找到.............");
        }
    }
}